﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CONEK911Manager.DTO;

namespace CONEK911Manager.DAO
{
    class ActiveDAO
    {
        readonly string _connectionString = ConfigurationManager.ConnectionStrings["Conek911ConnectionString"].ConnectionString;
        public bool AddActive(ActiveDTO active)
        {
            try
            {
                using (var con = new SqlConnection(_connectionString))
                {
                    const string sql = "INSERT into [Active] (ActiveLevel, ActiveMoney, PercentF1, PercentF2) VALUES (@activeLevel, @activeMoney, @percentF1, @percentF2)";
                    using (var sqlAddActive = new SqlCommand(sql))
                    {
                        sqlAddActive.Connection = con;
                        sqlAddActive.Parameters.Add(new SqlParameter("@activeLevel", active.ActiveLevel));
                        sqlAddActive.Parameters.Add(new SqlParameter("@activeMoney", active.ActiveMoney));
                        sqlAddActive.Parameters.Add(new SqlParameter("@percentF1", active.PercentF1));
                        sqlAddActive.Parameters.Add(new SqlParameter("@percentF2", active.PercentF2));
                        con.Open();
                        var result = sqlAddActive.ExecuteNonQuery();
                        con.Close();
                        if (result == 1)
                        {
                            return true;
                        }
                        return false;
                    }
                }
            } catch (Exception e)
            {
                try
                {
                    Logger.WriteLog(e.Message + "-" + e.StackTrace);
                }
                catch
                { }
                return false;
            }
        }

        public List<ActiveDTO> GetAllActive()
        {
            var listActive = new List<ActiveDTO>();
            using (var con = new SqlConnection(_connectionString))
            {
                const string sql = "SELECT * FROM [Active]";
                using (var cmd = new SqlCommand(sql))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var active = new ActiveDTO()
                                              {
                                                  ActiveLevel = Convert.ToString(reader["ActiveLevel"]),
                                                  ActiveMoney = double.Parse(Convert.ToString(reader["ActiveMoney"])),
                                                  PercentF1 = double.Parse(Convert.ToString(reader["PercentF1"])),
                                                  PercentF2 = double.Parse(Convert.ToString(reader["PercentF2"])),
                                              };
                            listActive.Add(active);
                        }
                    }
                    con.Close();
                }
            }

            return listActive;
        }
    }
}
